/*
 * Project: push-center-service-impl
 * 
 * File Created at 2014-10-14
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.push.service.core.tmpl.freemarker;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import javax.annotation.Resource;


import com.greenline.push.service.core.tmpl.TemplateCache;
import com.greenline.push.service.core.tmpl.TemplateInfo;

import freemarker.cache.TemplateLoader;

/**
 * DbTemplateLoader
 * 
 * @author weirui.shenwr
 * @date 2014-10-14
 * @version V1.0
 */
public class DbTemplateLoader implements TemplateLoader {

    // private static Log log = LogFactory.getLog(TemplateLoader.class);
//    private Logger log = Constants.LOG_PROCESS;

    @Resource
    private TemplateCache templateCache;

    @Override
    public Object findTemplateSource(String name) {
        TemplateInfo temp = templateCache.get(name);
        if (temp == null) {
            return null;
        } else {
            DBTemplateSource dbTemplate = new DBTemplateSource(name, temp.getText(), temp.getLastModified());
            return dbTemplate;
        }
    }

    @Override
    public long getLastModified(Object templateSource) {
        return ((DBTemplateSource) templateSource).lastModified;
    }

    @Override
    public Reader getReader(Object templateSource, String encoding) {
        return new StringReader(((DBTemplateSource) templateSource).source);
    }

    @Override
    public void closeTemplateSource(Object arg0) throws IOException {
    }

    // private Long toLong(String val) {
    // try {
    // return Long.valueOf(val);
    // } catch (Exception e) {
    // log.error(e);
    // return 0L;
    // }
    // }

    private static class DBTemplateSource {
        private final String id;
        private final String source;
        private final long lastModified;

        DBTemplateSource(String id, String source, long lastModified) {
            if (id == null) {
                throw new IllegalArgumentException("id == null");
            }
            if (source == null) {
                throw new IllegalArgumentException("source == null");
            }
            if (lastModified < -1L) {
                throw new IllegalArgumentException("lastModified < -1L");
            }
            this.id = id;
            this.source = source;
            this.lastModified = lastModified;
        }

        public boolean equals(Object obj) {
            if (obj instanceof DBTemplateSource) {
                DBTemplateSource tem = (DBTemplateSource) obj;
                return id.equals(tem.id);
            }
            return false;
        }

        public int hashCode() {
            return id.hashCode();
        }
    }

}
